package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by Administrator on 2015/4/29.
 */
public class ToastActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toast);

        Button btnDefault = (Button)this.findViewById(R.id.toast_btn_default);
        Button btnMove = (Button)this.findViewById(R.id.toast_btn_move);
        Button btnImage = (Button)this.findViewById(R.id.toast_btn_image);
        Button btnCustom = (Button)this.findViewById(R.id.toast_btn_custom);

        btnDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ToastActivity.this, "默认Toast", Toast.LENGTH_SHORT).show();
            }
        });

        btnMove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(ToastActivity.this, "移动位置的Toast", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL, -100, 100);
                toast.show();
            }
        });

        btnImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(ToastActivity.this, "带图片的Toast",Toast.LENGTH_SHORT);
                View toastView = toast.getView();
                LinearLayout toastLayout = (LinearLayout)toastView;
                ImageView imageView = new ImageView(ToastActivity.this);
                imageView.setImageResource(R.drawable.item);
                toastLayout.addView(imageView, 0);
                toast.show();
            }
        });

        btnCustom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = LayoutInflater.from(ToastActivity.this);
                View view = layoutInflater.inflate(R.layout.toast_layout, null);
                Toast toast = new Toast(ToastActivity.this);
                toast.setView(view);
                toast.show();
            }
        });
    }
}
